/**
 * @format
 * @Author: TC
 * @Date: 2021-10-02 13:45:42
 * @Last Modified by: TC
 * @Last Modified time: 2021-11-24 14:38:57
 */

import { _decorator, Camera, geometry, PhysicsRayResult, PhysicsSystem, v3 } from "cc";
import { BaseComponent } from "../../../FrameWork/Module/Component/BaseComponent";
import { gameDataMgr } from "../../_Common/Manager/GameDataManager";
const { ccclass, property } = _decorator;

@ccclass("RoleCamera")
export class RoleCamera extends BaseComponent {
    private ray: geometry.Ray = null;
    private unrealList: PhysicsRayResult[] = [];

    protected init(): void {
        super.init();
        gameDataMgr.mainCamera = this.getComponent(Camera);
        this.ray = new geometry.Ray();
        this.schedule(this.onRay);
    }

    update(dt: number): void {
        if (!gameDataMgr.roleNode) return;
        let pos = gameDataMgr.roleNode.getPosition().clone().add(v3(0, 10, 15.5));
        this.node.setPosition(pos);
    }

    /**
     * 射线检测
     * @param dt 定时器间隔
     */
    private onRay(dt: number): void {
        if (!gameDataMgr.roleNode) return;
        this.ray.d.set(-this.node.forward.x, -this.node.forward.y, -this.node.forward.z);
        this.ray.o = gameDataMgr.roleNode.getPosition().add3f(0, -1, 0);
        PhysicsSystem.instance.raycast(this.ray);
        this.unrealItems(PhysicsSystem.instance.raycastResults);
    }

    private unrealItems(results: PhysicsRayResult[]): void {
        if (results.length > 0) {
            results.forEach((item) => {
                if (this.unrealList.indexOf(item) > -1) {
                    return;
                }
                let node = item.collider.node;
                if (node.parent.getChildByName("obj")) {
                    this.unrealList.push(item);

                    // m.setProperty('albedo', color(255, 255, 255, 100));
                    node.parent.getChildByName("obj").active = false;
                    node.parent.getChildByName("obj2").active = true;
                }
            });
        } else {
            while (this.unrealList.length > 0) {
                let item = this.unrealList.pop();
                if (!item.collider.node.parent.getChildByName("obj")) {
                    return;
                }
                item.collider.node.parent.getChildByName("obj").active = true;
                item.collider.node.parent.getChildByName("obj2").active = false;
            }
        }
    }

    private shaking = false;
    private totalCnt = 10;
    private cnt = 0;
    /**
     * 震屏
     * @returns
     */
    public shake(): void {
        if (this.shaking) return;
        this.schedule(this.onShake, 0.04, this.totalCnt - 1, 0);
    }

    /**
     * 震屏计时器回调
     * @param dt
     */
    private onShake(dt: number): void {
        if (0 == this.cnt % 2) {
            this.node.setPosition(this.node.getPosition().clone().add(v3(-0.25, 0, 0)));
        } else {
            this.node.setPosition(this.node.getPosition().clone().add(v3(0.25, 0, 0)));
        }
        this.cnt++;
        if (this.cnt == this.totalCnt) {
            this.unschedule(this.onShake);
            this.shaking = false;
        }
    }
}
